package com.kaifamiao.month03.day0329;

public class Search {

    public static int binarySearch(int[] arr, int key, int start, int end) {
        if (start > end)
            return -1;
        int mid = (start + end) >> 1;
        if (arr[mid] == key)
            return mid;
        else if (arr[mid] < key)
            start = mid + 1;
        else
            end = mid - 1;
        return binarySearch(arr, key, start, end);
    }

    public static int binarySearch(int[] arr, int key) {
        int start = 0;
        int end = arr.length - 1;
        while (start <= end) {
            int mid = (start + end) >> 1;
            if (arr[mid] == key)
                return mid;
            else if (arr[mid] < key)
                start = mid + 1;
            else
                end = mid - 1;
        }
        return -1;
    }

    public static void main(String[] args) {
        int[] arr = {1, 2, 3, 4, 5, 6, 7};
//        System.out.println(binarySearch(arr,3,0,arr.length - 1));
        System.out.println(binarySearch(arr, 2));
    }

}
